package db

import (
	"strconv"
	"strings"
	"sync"

	"github.com/go-redis/redis"
)

var redis_once sync.Once
var db map[string]*redis.Client

func RedisInit(config []string) {

	redis_once.Do(func() {
		db = map[string]*redis.Client{}
		//name@address@passwd@db
		for _, v := range config {
			config := strings.Split(v, "@")
			var Name, Addr, Password string
			DB := 1
			if len(config) >= 1 {
				Name = config[0]
			}

			if len(config) >= 2 {
				Addr = config[1]
			}

			if len(config) >= 3 {
				Password = config[2]
			}

			if len(config) >= 4 {
				DB, _ = strconv.Atoi(config[3])
			}

			db[Name] = redis.NewClient(&redis.Options{
				Addr:     Addr,
				Password: Password,
				DB:       DB,
			})
		}
	})

}

func GetRedisClient(name string) *redis.Client {
	return db[name]
}
